import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'loginUser',
    component: () => import(/* webpackChunkName: "about" */ '../views/loginUser.vue')
  },
  {
    path: '/loginUser',
    name: 'loginUser',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/loginUser.vue')
  },
  {
    path: '/homePage',
    name: 'homePage',
    redirect: '/welcomeTo',
    component: () => import(/* webpackChunkName: "about" */ '../views/homePage.vue'),
    children: [
      { path: '/welcomeTo', name: 'welcomeTo', component: () => import(/* webpackChunkName: "about" */ '../views/welcomeTo.vue') },
      { path: '/progarm/rotateList', name: 'rotateList', component: () => import(/* webpackChunkName: "progarm" */ '../views/progarm/rotateList.vue') },
      { path: '/progarm/gridList', name: 'gridList', component: () => import(/* webpackChunkName: "progarm" */ '../views/progarm/gridList.vue') },
      { path: '/user/userList', name: 'userList', component: () => import(/* webpackChunkName: "about" */ '../views/user/userList.vue') },
      { path: '/user/adminList', name: 'adminList', component: () => import(/* webpackChunkName: "about" */ '../views/user/adminList.vue') },
      { path: '/health/clockList', name: 'clockList', component: () => import(/* webpackChunkName: "about" */ '../views/health/clockList.vue') },
      { path: '/health/signList', name: 'signList', component: () => import(/* webpackChunkName: "about" */ '../views/health/signList.vue') },
      { path: '/school/requestList', name: 'requestList', component: () => import(/* webpackChunkName: "about" */ '../views/school/requestList.vue') },
      { path: '/school/returnList', name: 'returnList', component: () => import(/* webpackChunkName: "about" */ '../views/school/returnList.vue') },
      { path: '/school/inList', name: 'inList', component: () => import(/* webpackChunkName: "about" */ '../views/school/inList.vue') },
      { path: '/notice/noticeList', name: 'noticeList', component: () => import(/* webpackChunkName: "about" */ '../views/notice/noticeList.vue') },
      { path: '/personal/myCenter', name: 'myCenter', component: () => import(/* webpackChunkName: "about" */ '../views/personal/myCenter.vue') },
      { path: '/personal/logList', name: 'logList', component: () => import(/* webpackChunkName: "about" */ '../views/personal/logList.vue') }
    ]
  }

]

const router = new VueRouter({
  routes
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to将要访问的路径,from从哪个路径跳转而来，next函数表示放行
  if (to.path === '/loginUser') return next()
  // 从sessionStorage中获取token,没有token就强制跳转登录页面，有则放行
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/loginUser')
  next()
})

export default router
